package com.aerozhonghuan.logisticsmall.modules.mine.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.aerozhonghuan.foundation.log.LogUtil;
import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.TitlebarFragment;
import com.aerozhonghuan.logisticsmall.modules.mine.adapter.MessageAdapter;
import com.aerozhonghuan.logisticsmall.modules.mine.beans.MessageListBean;
import com.aerozhonghuan.logisticsmall.modules.mine.logic.MineWebRequest;
import com.aerozhonghuan.logisticsmall.widget.ProgressDialogIndicator;
import com.aerozhonghuan.oknet2.CommonCallback;
import com.aerozhonghuan.oknet2.CommonMessage;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.List;

/**
 * 描述 个人中心页面
 *
 * @author zhangyonghui
 * 创建日期 2019-05-23
 */
public class MessageFragment extends TitlebarFragment {

    private static final String TAG = "MessageFragment";
    private static final int TYPE_LOADDATA_FIRST = 0;
    private static final int TYPE_LOADDATA_REFRESH = 1;
    private static final int TYPE_LOADDATA_LOADMORE = 2;
    private View rootView;
    private ProgressDialogIndicator progressDialogIndicator;
    private XRecyclerView recylerMessages;
    private MessageAdapter messageAdapter;
    private boolean isFirstLoadData = true;
    private int currentPage = 1;
    private int totalPage;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_me_message, null);
            initView();
            initData(TYPE_LOADDATA_FIRST, 1);
        }
        return rootView;
    }

    private void initView() {
        progressDialogIndicator = new ProgressDialogIndicator(getContext());
        recylerMessages = (XRecyclerView) rootView.findViewById(R.id.recylerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recylerMessages.setLayoutManager(layoutManager);
        messageAdapter = new MessageAdapter(getContext());
        recylerMessages.setFootViewText("正在加载更多...", "暂无更多数据");
        recylerMessages.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        recylerMessages.setLoadingMoreProgressStyle(ProgressStyle.Pacman);
        recylerMessages.setAdapter(messageAdapter);
        recylerMessages.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                initData(TYPE_LOADDATA_REFRESH, 1);
            }

            @Override
            public void onLoadMore() {
                if (currentPage >= totalPage) {
                    recylerMessages.setNoMore(true);
                } else {
                    initData(TYPE_LOADDATA_LOADMORE, currentPage + 1);
                }
            }
        });
        LogUtil.d(TAG, "recyler State1::" + recylerMessages.getDefaultRefreshHeaderView().getState());
    }

    private void initData(final int type, int loadPage) {
        if (isFirstLoadData) {
            progressDialogIndicator.onProgressStart();
        }
        MineWebRequest.queryMessageList(getContext(), loadPage, new CommonCallback<MessageListBean>() {
            @Override
            public void onSuccess(MessageListBean messageListBean, CommonMessage commonMessage, String allResponseString) {
                if (isFirstLoadData) {
                    progressDialogIndicator.onProgressEnd();
                    isFirstLoadData = false;
                }
                if (messageListBean != null) {
                    totalPage = messageListBean.getTotalPage();
                    List<MessageListBean.MessageBean> datalist = messageListBean.getDatalist();
                    switch (type) {
                        case TYPE_LOADDATA_FIRST:
                            messageAdapter.refreshList(datalist);
                            break;
                        case TYPE_LOADDATA_REFRESH:
                            messageAdapter.refreshList(datalist);
                            recylerMessages.refreshComplete();
                            currentPage = 1;
                            break;
                        case TYPE_LOADDATA_LOADMORE:
                            messageAdapter.addList(datalist);
                            recylerMessages.loadMoreComplete();
                            currentPage++;
                            break;
                        default:
                            break;
                    }
                }
            }

            @Override
            public boolean onFailure(int httpCode, Exception exception, CommonMessage responseMessage, String allResponseString) {
                if (isFirstLoadData) {
                    progressDialogIndicator.onProgressEnd();
                    isFirstLoadData = false;
                }
                return super.onFailure(httpCode, exception, responseMessage, allResponseString);
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (recylerMessages != null) {
            recylerMessages.destroy();
            recylerMessages = null;
        }
    }
}
